﻿namespace Mediator
{
    internal class LoginFrameobsolete : Form, IMediator
    {
        private ColleagueCheckbox checkGuest;
        private ColleagueCheckbox checkLogin;
        private ColleagueTextField textUser;
        private ColleagueTextField textPassword;
        private ColleagueButton buttonOk;
        private ColleagueButton buttonCancel;

        public LoginFrameobsolete(string title)
        {
            this.Text = title;

            var panel = new FlowLayoutPanel();

            this.BackColor = Color.LightGray;
            this.SizeGripStyle = SizeGripStyle.Auto;

            this.CreateColleagues();

            panel.Controls.Add(checkGuest);
            panel.Controls.Add(checkLogin);
            panel.Controls.Add(new Label { Text= "Username:" });
            panel.Controls.Add(textUser);
            panel.Controls.Add(new Label { Text = "Password:" });
            panel.Controls.Add(textPassword);
            panel.Controls.Add(buttonOk);
            panel.Controls.Add(buttonCancel);
            this.Controls.Add(panel);   
            this.ColleagueChanged();
        }

        public void CreateColleagues()
        {
            var g = new Panel();
            this.checkGuest = new ColleagueCheckbox("Guest", true);
            this.checkLogin = new ColleagueCheckbox("Login", true);
            g.Controls.Add(this.checkGuest);
            g.Controls.Add(this.checkLogin);

            this.textUser = new ColleagueTextField("", 10);
            this.textPassword = new ColleagueTextField("Password", 10)
            {
                PasswordChar = '*'
            };

            this.buttonOk = new ColleagueButton("OK");
            this.buttonCancel = new ColleagueButton("Cancel");

            // 设置mediator
            this.checkGuest.SetMediator(this);
            this.checkLogin.SetMediator(this);
            this.textUser.SetMediator(this);
            this.textPassword.SetMediator(this);
            this.buttonOk.SetMediator(this);
            this.buttonCancel.SetMediator(this);

            // 

        }

        public void ColleagueChanged()
        {
            if (this.checkGuest.Enabled)
            {
                textUser.SetColleagueEnabled(false);
                textPassword.SetColleagueEnabled(false);
                buttonOk.SetColleagueEnabled(true);
            }
            else
            {
                textUser.SetColleagueEnabled(true);
                this.UserPasswordChanged();
            }
        }

        private void UserPasswordChanged()
        {
            if (textUser.Text.Length > 0)
            {
                textPassword?.SetColleagueEnabled(true);
                buttonOk?.SetColleagueEnabled(textPassword?.Text.Length > 0);
            }
            else 
            {
                textPassword?.SetColleagueEnabled(false);
                buttonOk?.SetColleagueEnabled(true);
            }
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            CheckBoxGroup = new GroupBox();
            LabelUserName = new Label();
            LabelPassword = new Label();
            SuspendLayout();
            // 
            // CheckBoxGroup
            // 
            CheckBoxGroup.Location = new Point(12, 12);
            CheckBoxGroup.Name = "CheckBoxGroup";
            CheckBoxGroup.Size = new Size(546, 311);
            CheckBoxGroup.TabIndex = 0;
            CheckBoxGroup.TabStop = false;
            // 
            // LabelUserName
            // 
            LabelUserName.AutoSize = true;
            LabelUserName.Location = new Point(35, 109);
            LabelUserName.Name = "LabelUserName";
            LabelUserName.Size = new Size(119, 28);
            LabelUserName.TabIndex = 1;
            LabelUserName.Text = "UserName";
            // 
            // LabelPassword
            // 
            LabelPassword.AutoSize = true;
            LabelPassword.Location = new Point(46, 168);
            LabelPassword.Name = "LabelPassword";
            LabelPassword.Size = new Size(108, 28);
            LabelPassword.TabIndex = 3;
            LabelPassword.Text = "Password";
            // 
            // CheckLogin
            // 
            CheckLogin.Location = new Point(12, 12);
            CheckLogin.Name = "CheckLogin";
            CheckLogin.Size = new Size(104, 24);
            CheckLogin.TabIndex = 0;
            CheckLogin.Text = "Login";
            // 
            // TextUser
            // 
            TextUser.Location = new Point(0, 0);
            TextUser.Name = "TextUser";
            TextUser.Size = new Size(100, 34);
            TextUser.TabIndex = 0;
            // 
            // TextPassword
            // 
            TextPassword.Location = new Point(0, 0);
            TextPassword.Name = "TextPassword";
            TextPassword.Size = new Size(100, 34);
            TextPassword.TabIndex = 0;
            // 
            // LoginFrame
            // 
            AutoScaleDimensions = new SizeF(13F, 28F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(568, 395);
            Controls.Add(LabelPassword);
            Controls.Add(LabelUserName);
            Controls.Add(CheckBoxGroup);
            Name = "LoginFrame";
            Text = "Form1";
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private GroupBox CheckBoxGroup;
        private ColleagueCheckbox CheckLogin;
        private ColleagueCheckbox CheckGuest;
        private Label LabelUserName;
        private ColleagueTextField TextUser;
        private Label LabelPassword;
        private ColleagueTextField TextPassword;
        private ColleagueButton ButtonOK;
        private ColleagueButton ButtonCancel;
    }
}
